﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using pay.DAL;
using System.Data;

namespace TG
{
    /// <summary>
    ///TG 的摘要说明
    /// </summary>
    public class TG
    {
        /// <summary>
        /// 主键
        /// </summary>
        public long Id
        {
            get;
            set;
        }

        /// <summary>
        /// 手机号码
        /// </summary>
        public string Mobile
        {
            get;
            set;
        }

        /// <summary>
        /// 姓名
        /// </summary>
        public string Name
        {
            get;
            set;
        }

        /// <summary>
        /// 用户名
        /// </summary>
        public string UserName
        {
            get;
            set;
        }

        /// <summary>
        /// 软件密码
        /// </summary>
        public string Pwd
        {
            get;
            set;
        }

        /// <summary>
        /// 订单编号
        /// </summary>
        public string OrderNumber
        {
            get;
            set;
        }

        public bool? IsZhiFu
        {
            get;
            set;
        }

        public int Count
        {
            get;
            set;
        }
    }

    public class TGBLL
    {
        public void UpdateTg(string orderNumber)
        {
             TGDal dal = new TGDal();
             TG tg = dal.GetTgByOrderNumber(orderNumber);
         
              tg.UserName = "";
              tg.Pwd = "";
              WebService.emoneybusiness service = new WebService.emoneybusiness();
               for (int i = 0; i < tg.Count; i++)
               {
                   string[] strs = service.TGUserPwd(tg.Mobile);
                   tg.UserName += strs[0]+"|";
                   tg.Pwd += strs[1]+"|";
                   string tt="";
                   if(tg.Count>1)
                   {
                       tt="第"+(i+1)+"个";
                   }
                   string content = "尊敬的" + tg.Name + "：您团购软件用户名是："+strs[0]+"，密码：" + strs[1] + "，请登录18.emoney.cn下载软件";
                   service.SendMsg(tg.Mobile, content);
               }
               if (tg.Count > 0)
               {
                   tg.UserName = tg.UserName.Substring(0, tg.UserName.Length - 1);
                   tg.Pwd = tg.Pwd.Substring(0, tg.Pwd.Length - 1);
               }
            dal.Update(tg);
        }
    }

    public class TGDal
    {

        public TG GetTgByOrderNumber(string orderNumber)
        {
            string sql = "select * from TG where OrderNumber=@ordernumber";
            Tools tool = new Tools();
            tool.AddSqlParameter("ordernumber", System.Data.SqlDbType.VarChar, orderNumber);
            DataTable tb= SQLHelper.ExecuteSet(DBConn.Temporary, System.Data.CommandType.Text, sql, tool.GetSqlParameters()).Tables[0];
            if (tb.Rows.Count > 0)
            {
                DataRow row = tb.Rows[0];
                TG tg = new TG();
                tg.Id = Tools.GetLong(row["id"]).Value;
                tg.IsZhiFu = Convert.ToBoolean(row["IsZhiFu"]);
                tg.Mobile = Convert.ToString(row["mobile"]);
                tg.Name = Convert.ToString(row["name"]);
                tg.OrderNumber = Convert.ToString(row["OrderNumber"]);
                tg.Pwd = Convert.ToString(row["pwd"]);
                tg.UserName = Convert.ToString(row["username"]);
                try
                {
                    tg.Count = Tools.GetInt(row["count"]).Value;
                }
                catch
                {
                }
                return tg;
            }
            else
            {
                return null;
            }
        }

        public TG GetTgByMobile(string mobile)
        {
            string sql = "select * from TG where IsZhiFu=1 and mobile=@mobile";
            Tools tool = new Tools();
            tool.AddSqlParameter("mobile", SqlDbType.VarChar, mobile);
            DataTable tb = SQLHelper.ExecuteSet(DBConn.Temporary, System.Data.CommandType.Text, sql, tool.GetSqlParameters()).Tables[0];
            if (tb.Rows.Count > 0)
            {
                DataRow row = tb.Rows[0];
                TG tg = new TG();
                tg.Id = Tools.GetLong(row["id"]).Value;
                tg.IsZhiFu = Convert.ToBoolean(row["IsZhiFu"]);
                tg.Mobile = Convert.ToString(row["mobile"]);
                tg.Name = Convert.ToString(row["name"]);
                tg.OrderNumber = Convert.ToString(row["OrderNumber"]);
                tg.Pwd = Convert.ToString(row["pwd"]);
                tg.UserName = Convert.ToString(row["username"]);
                try
                {
                    tg.Count = Tools.GetInt(row["count"]).Value;
                }
                catch
                {
                }
                return tg;
            }
            else
            {
                return null;
            }
        }

        public void Add(TG c)
        {
            string sql = "insert into TG (mobile,name,OrderNumber,IsZhiFu,count) values(@mobile,@name,@OrderNumber,0,@count)";
            Tools tool = new Tools();
            tool.AddSqlParameter("mobile", System.Data.SqlDbType.VarChar, c.Mobile);
            tool.AddSqlParameter("name", System.Data.SqlDbType.VarChar, c.Name);
            tool.AddSqlParameter("OrderNumber", System.Data.SqlDbType.VarChar, c.OrderNumber);
            tool.AddSqlParameter("count", System.Data.SqlDbType.Int, c.Count);
            SQLHelper.ExecuteNonQuery(DBConn.Temporary, System.Data.CommandType.Text, sql, tool.GetSqlParameters());
        }

        public void Update(TG c)
        {
            string sql = "update TG set IsZhiFu=1,username=@username,pwd=@pwd where OrderNumber=@ordernumber";
            Tools tool = new Tools();
            tool.AddSqlParameter("username", System.Data.SqlDbType.VarChar, c.UserName);
            tool.AddSqlParameter("pwd", System.Data.SqlDbType.VarChar, c.Pwd);
            tool.AddSqlParameter("ordernumber", System.Data.SqlDbType.VarChar, c.OrderNumber);
            SQLHelper.ExecuteNonQuery(DBConn.Temporary, System.Data.CommandType.Text, sql, tool.GetSqlParameters());
        }
    }
}